package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.action.back.param.RouteQueryParam;
import com.qifan.alb2b.domain.RouteInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 旅游线路CURD
 * @author hc24
 *
 */
@Repository
public class RouteInfoDao extends BaseDao {
	private final static Log logger=LogFactory.getLog(RouteInfoDao.class);
	
	/**
	 * 根据条件分页查询线路信息
	 * @param dgm 分页参数
	 * @param gid 线路查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,RouteQueryParam rqp){
		logger.debug("线路查询");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from routeinfo where 1=1");
		if(rqp.getRouteName()!=null&&!"".equals(rqp.getRouteName().trim())){
			sql.append(" and routeName like ?");
			params.add("%"+rqp.getRouteName()+"%");
		}
		if(rqp.getOrgCityId()!=null&&!"".equals(rqp.getOrgCityId().trim())){
			sql.append(" and orgCityId=?");
			params.add(rqp.getOrgCityId());
		}
		if(rqp.getDstCityId()!=null&&!"".equals(rqp.getDstCityId().trim())){
			sql.append(" and dstCityId=?");
			params.add(rqp.getDstCityId());
		}
		if(rqp.getStartDay()!=-1){
			sql.append(" and day>=?");
			params.add(rqp.getStartDay());
		}
		if(rqp.getEndDay()!=-1){
			sql.append(" and day<=?");
			params.add(rqp.getEndDay());
		}		
		if(rqp.getHot()!=-1){
			sql.append(" and hot = ?");
			params.add(rqp.getHot());
		}
		if(rqp.getCountryName()!=null&&!"".equals(rqp.getCountryName().trim())){
			sql.append(" and countryName=?");
			params.add(rqp.getCountryName());
		}
		if(rqp.getStatus()!=-1){
			sql.append(" and status=?");
			params.add(rqp.getStatus());
		}
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, RouteInfoDomain.class);
	}
	
	public RouteInfoDomain getRouteById(String rid){
		String sql="select * from routeInfo where routeId=?";
		List<RouteInfoDomain> list=JdbcUtil.queryForList(this, RouteInfoDomain.class, sql, rid);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 根据routeId查询管理的订单数量
	 * @param routeId
	 * @return
	 */
	public int getOrderCountByRoute(String routeId){
		String sql="select count(*) from orderInfo where routeId=?";
		return this.getJdbcTemplate().queryForInt(sql, routeId);
	}
	
	public void add(RouteInfoDomain routeInfoDomain){
		super.addObj(routeInfoDomain);
	}
	
	public void update(RouteInfoDomain routeInfoDomain){
		super.updateObj(routeInfoDomain, "routeId");
	}
	
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from routeInfo where routeId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		logger.debug("删除sql："+delSql+"["+ids+"]");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
}
